package hanelsoft.vn.dictionaylearn.control.activity;

import hanelsoft.vn.dictionaylearn.R;
import hanelsoft.vn.dictionaylearn.control.custom.CustomHistory;
import hanelsoft.vn.dictionaylearn.model.db.DatabaseFile;
import hanelsoft.vn.dictionaylearn.model.db.DatabaseFileList;
import hanelsoft.vn.dictionaylearn.model.db.VocabularySQLiteHelper;
import hanelsoft.vn.dictionaylearn.model.entity.InfoHistory;

import java.util.ArrayList;

import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ImageButton;
import android.widget.ListView;

public class History extends Activity{
	ArrayList<InfoHistory> array;
	InfoHistory info;
	ListView list;
	ImageButton btnDelete, btnCancel;
	private String mDBExtension;
	private String mDBPath;
	private SharedPreferences prefs;
	private DatabaseFileList mDBList;
	private DatabaseFile mDBFile;
	boolean found = false;
	Intent mIntent;
	SharedPreferences share;
	VocabularySQLiteHelper mVocabularySQLiteHelper = new VocabularySQLiteHelper(this);
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		//requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.history);
		array = new ArrayList<InfoHistory>();
		list = (ListView)findViewById(R.id.history_list);
		btnDelete = (ImageButton)findViewById(R.id.history_delete);
		btnCancel = (ImageButton)findViewById(R.id.history_cancel);
		list.setAdapter(new CustomHistory(this, getInfoHistory()));
		list.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> arg0, View v, int position, long arg3) {
    			Intent i = new Intent(v.getContext(), ContentView.class);
    			info = getInfoHistory().get(position);
    			i.putExtra("word", info.getWord());
    			i.putExtra("id",info.getId_word());
    			//get position data dictionary
    	        mDBExtension = getResources().getString(R.string.dbExtension);
    	        mDBPath = getResources().getString(R.string.dbPath);
    	        mDBList = new DatabaseFileList(mDBPath,mDBExtension);
    	        prefs = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
    	        String savedDB = prefs.getString("defaultDictionary", ""); //Default dictionary is the first dictionary in the list
    			String savedDBPath = prefs.getString("defaultDictionaryPath", ""); //Default dictionary is the first dictionary in the list
    			if (savedDB.trim().equals("") || savedDBPath.trim().equals(""))
    			{
    				if (mDBList != null && mDBList.items.size() > 0)
    				{
    					mDBFile = mDBList.items.get(0);
    				}
    				else
    				{
    					mDBFile = null;
    				}
    			}
    			else
    			{
    				if (mDBList != null && mDBList.items.size() > 0)
    				{
    					for (DatabaseFile d : mDBList.items)
    					{
    						if (d.fileName.equals(savedDB) && d.path.equals(savedDBPath))
    						{
    							mDBFile = d;
    							found = true;
    							break;
    						}
    					}
    					if (!found)
    					{
    						mDBFile = mDBList.items.get(0);
    					}
    				}
    				else
    				{
    					mDBFile = null;
    				}
    			}
    			i.putExtra("db", mDBFile.fileName);
    			i.putExtra("dbName",mDBFile.dictionaryName);
    			i.putExtra("style", mDBFile.style);
    			i.putExtra("sourcesound", mDBFile.sourceLanguage);
                startActivity(i);
			}
		});
		btnDelete.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				mVocabularySQLiteHelper.deleteHistory();
				list.setAdapter(new CustomHistory(History.this, getInfoHistory()));
			}
		});
		btnCancel.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				finish();
			}
		});
	}
	private ArrayList<InfoHistory> getInfoHistory() {
		array = mVocabularySQLiteHelper.viewAllHistory();
		return array;
	}
}

